﻿using MyHospital.BLL;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyHospital.web
{
    public partial class ManageDisease : System.Web.UI.Page
    {
        Disease disease = new Disease();
        protected void Page_Load(object sender, EventArgs e)
        {
            InsertTitle.Visible = false;
            InsertLableName.Visible = false;
            txtInsertName.Visible = false;
            InsertLableDepart.Visible = false;
            departSelect1.Visible = false;
            InsertSubmit.Visible = false;
            UpdateTitle.Visible = false;
            UpdateLableName.Visible = false;
            txtUpdateName.Visible = false;
            UpdateLableDepart.Visible = false;
            departSelectUpdate.Visible = false;
            UpdateSubmit.Visible = false;
        }

        protected void insertDisease_Click(object sender, EventArgs e)//点击插入按钮能看到的页面
        {
            lblMassage1.Text = "";
            lblMassage.Text = "";
            InsertTitle.Visible = true;
            InsertLableName.Visible = true;
            txtInsertName.Visible = true;
            InsertLableDepart.Visible = true;
            departSelect1.Visible = true;
            InsertSubmit.Visible = true;
        }

        protected void updateDisease_Click(object sender, EventArgs e)//点击修改按钮能看到的页面
        {
            lblMassage1.Text = "";
            lblMassage.Text = "";
            if (Request.Form["myRadio"] == null)
            {
                lblMassage1.Text = "没有选中项";
                jsLiteral.Text = "";
            }
            else
            {
                lblMassage1.Text = "";
                UpdateTitle.Visible = true;
                UpdateLableName.Visible = true;
                txtUpdateName.Visible = true;
                UpdateLableDepart.Visible = true;
                departSelectUpdate.Visible = true;
                UpdateSubmit.Visible = true;
                string value;
                value = Request.Form["myRadio"].ToString();
                jsLiteral.Text = "setNowRadio('" + value + "');";
                txtUpdateName.Text=DiseaseList.Rows[Convert.ToInt16(value)].Cells[1].Text;
                txtUpdateName.ReadOnly = true;
            }
        }
        protected void deleteDisease_Click(object sender, EventArgs e)//删除一条数据
        {
            lblMassage1.Text = "";
            lblMassage.Text = "";
            if (Request.Form["myRadio"] == null)
            {
                lblMassage1.Text = "没有选中项";
                jsLiteral.Text = "";
            }
            else
            {
                string value;
                value = Request.Form["myRadio"].ToString();
                jsLiteral.Text = "setNowRadio('" + value + "');";
                disease.deleteDisease(DiseaseList.Rows[Convert.ToInt16(value)].Cells[0].Text);
                lblMassage.Text = DiseaseList.Rows[Convert.ToInt16(value)].Cells[0].Text + "号疾病删除成功";
                DiseaseList.DataSourceID = "lqsDisease";
                DiseaseList.DataBind();
            }
        }
        protected void InsertSubmit_Click(object sender, EventArgs e)//插入数据，可选择多个部门
        {
            lblMassage1.Text = "";
            lblMassage.Text = "";
            InsertTitle.Visible = true;
            InsertLableName.Visible = true;
            txtInsertName.Visible = true;
            InsertLableDepart.Visible = true;
            departSelect1.Visible = true;
            InsertSubmit.Visible = true;
            if (txtInsertName.Text == String.Empty) lblMassage.Text = "名称不能为空！";
            else if(Session["depart"] == null) lblMassage.Text = "请选择部门！";
            else if(txtInsertName.Text != String.Empty)
            {
                if (disease.isDiseaseExit(txtInsertName.Text) == true)
                {
                    string[] sDepart = Session["depart"].ToString().Split(',');
                    for (int i = 0; i < sDepart.Length; i++)
                    {
                        int ID;
                        bool b = int.TryParse(sDepart[i], out ID);
                        if (b==true)
                        {
                            disease.insertDisease(ID, txtInsertName.Text);
                        }
                    }
                    DiseaseList.DataSourceID = "lqsDisease";
                    DiseaseList.DataBind();
                    lblMassage.Text = "插入成功！";
                }
                else lblMassage.Text = "疾病已存在！";
            }
            Session.Remove("depart");
        }

        protected void UpdateSubmit_Click(object sender, EventArgs e)//修改疾病对应的部门
        {
            lblMassage1.Text = "";
            lblMassage.Text = "";
            UpdateTitle.Visible = true;
            UpdateLableName.Visible = true;
            txtUpdateName.Visible = true;
            UpdateLableDepart.Visible = true;
            departSelectUpdate.Visible = true;
            UpdateSubmit.Visible = true;
            if (Session["depart"] == null) lblMassage.Text = "请选择部门！";
            else
            {
                disease.deleteAllDisease(txtUpdateName.Text);//删除原有数据
                string[] sDepart = Session["depart"].ToString().Split(',');
                for (int i = 0; i < sDepart.Length; i++)
                {
                    int ID;
                    bool b = int.TryParse(sDepart[i], out ID);
                    if (b == true)
                    {
                        disease.insertDisease(ID, txtUpdateName.Text);
                    }
                }
                DiseaseList.DataSourceID = "lqsDisease";
                DiseaseList.DataBind();
                lblMassage.Text = "修改成功！";
            }
            Session.Remove("depart");
        }
        protected void departInsertSubmit_Click(object sender, EventArgs e)//获取需要插入的部门
        {
            Session.Remove("depart");
            foreach (ListItem listItem in chkDepart.Items)
            {
                if (listItem.Selected)
                {
                    Session["depart"] = Session["depart"] + listItem.Value+",";
                }
            }
            InsertTitle.Visible = true;
            InsertLableName.Visible = true;
            txtInsertName.Visible = true;
            InsertLableDepart.Visible = true;
            departSelect1.Visible = true;
            InsertSubmit.Visible = true;
        }

        protected void deleteAllDisease_Click(object sender, EventArgs e)//删除某疾病对应的所以数据
        {
            lblMassage1.Text = "";
            lblMassage.Text = "";
            if (Request.Form["myRadio"] == null)
            {
                lblMassage1.Text = "没有选中项";
                jsLiteral.Text = "";
            }
            else
            {
                string value;
                value = Request.Form["myRadio"].ToString();
                jsLiteral.Text = "setNowRadio('" + value + "');";
                disease.deleteAllDisease(DiseaseList.Rows[Convert.ToInt16(value)].Cells[1].Text);
                lblMassage.Text = DiseaseList.Rows[Convert.ToInt16(value)].Cells[1].Text + "疾病删除成功";
                DiseaseList.DataSourceID = "lqsDisease";
                DiseaseList.DataBind();
            }
        }

        protected void departUpdateSubmit_Click(object sender, EventArgs e)//获取修改之后的部门
        {
            lblMassage1.Text = "";
            lblMassage.Text = "";
            Session.Remove("depart");
            foreach (ListItem listItem in chkUpdateDepart.Items)
            {
                if (listItem.Selected)
                {
                    Session["depart"] = Session["depart"] + listItem.Value + ",";
                }
            }
            lblMassage1.Text = "";
            UpdateTitle.Visible = true;
            UpdateLableName.Visible = true;
            txtUpdateName.Visible = true;
            UpdateLableDepart.Visible = true;
            departSelectUpdate.Visible = true;
            UpdateSubmit.Visible = true;
            string value;
            value = Request.Form["myRadio"].ToString();
            jsLiteral.Text = "setNowRadio('" + value + "');";
            txtUpdateName.Text = DiseaseList.Rows[Convert.ToInt16(value)].Cells[1].Text;
            txtUpdateName.ReadOnly = true;
        }
        protected void departSelectUpdate_Click(object sender, EventArgs e)//获取所选疾病对应的部门
        {

            IQueryable<int?> depart = disease.searchDepart(txtUpdateName.Text);
            for (int i = 0; i < depart.Count(); i++)
            {
                foreach (ListItem listItem in chkUpdateDepart.Items)
                {
                    if (int.Parse(listItem.Value) == depart.ElementAtOrDefault(i))
                    {
                        listItem.Selected = true;
                    }
                }
            }
        }

        protected void DepartList_SelectedIndexChanged(object sender, EventArgs e)
        {
            int page = DepartList.PageIndex;
            if (Session["indexDisease"] != null && Session["pageDisease"].ToString() == page.ToString())
            {
                DepartList.Rows[Convert.ToInt16(Session["indexDisease"].ToString())].ForeColor = Color.Empty;
                DepartList.Rows[Convert.ToInt16(Session["indexDisease"].ToString())].BackColor = Color.Empty;
            }
            int index = DepartList.SelectedIndex;//获得所选择的医生下标
            DepartList.Rows[Convert.ToInt16(index)].ForeColor = Color.Black;
            DepartList.Rows[Convert.ToInt16(index)].BackColor = ColorTranslator.FromHtml("#ccccd4");
            Session["indexDisease"] = index.ToString();
            Session["pageDisease"] = page.ToString();
        }
    }
}